package pl.icn.hurtownia.mappings.converters;

import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.dozer.DozerConverter;

/**
 * Globalny konwerter Date <-> XMLGregorianCalendar
 *
 */
public class DateCustomConverter extends DozerConverter<java.util.Date, javax.xml.datatype.XMLGregorianCalendar> {
	
	public static final String NAME = "dateCustomConverter";
	
	/**
	 * Tworzy nową instancję typu <code>DateCustomConverter</code>.
	 * 
	 * @param prototypeA
	 * @param prototypeB
	 */
	public DateCustomConverter() {
		super(java.util.Date.class, javax.xml.datatype.XMLGregorianCalendar.class);
	}

	@Override
	public XMLGregorianCalendar convertTo(Date source, XMLGregorianCalendar destination) {
		if (source == null) {
			return null;
		}
		GregorianCalendar c = new GregorianCalendar();
		c.setTime(source);
		try {
			destination = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
		} catch (DatatypeConfigurationException e) {
			e.printStackTrace();
		}
		return destination;
	}

	@Override
	public Date convertFrom(XMLGregorianCalendar source, Date destination) {
		if (source == null) {
			return null;
		}
		destination = source.toGregorianCalendar().getTime();
		return destination;
	}
	
}